home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7762 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  46 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: automatic charrs
  5. Date: 28 Feb 1996 14:44:04 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4h2ba4$ft0@umbc9.umbc.edu>
  8. References: <4glp29$dsh@d2.tufts.edu>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Roberto Dorich <rdorich@emerald.tufts.edu> wrote:
  13. |> Could someone enlighten me, and tell me why the following program runs
  14. |> just dandy using Sun's cc (3.0.1), but crashes using gcc (?) ?
  15.  
  16. Dumb luck? Writing illegal code usually produces unpredictable behavior
  17. which sometimes turns out to be what was originally expected. This does
  18. not mean you will always be so lucky.
  19.  
  20. |> #include <stdio.h>
  21. |> char *b() { return "Hello";}
  22. |>  
  23. |> main() {
  24. |>   char *a=b();
  25.  
  26. No problem there...
  27.  
  28. |>   a[0]='X';
  29.  
  30. Here's the culprit! The string "Hello" is constant and as such is not legally
  31. modified.
  32.  
  33. |>   printf("--> %s\n",a);
  34. |> }
  35.  
  36. <rest deleted>
  37.  
  38. I don't even know if your Sun compiler is ANSI because your functions
  39. parameters were not explicitly defined. However, either make "Hello" a local
  40. static array of char or find some other way of doing whatever it si you
  41. are trying to do.
  42. -- 
  43. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  44.  
  45. Jonas J. Schlein  (schlein@gl.umbc.edu)
  46.